<?php
	class mestatus extends Model {
		private $mTableName = 'SC_Estatus';
	
		public function __construct() 
		{
			parent::Model();
			$this->load->database();
		}
		
		function CreateEstatus($nombre,$desc,$dias) 
		{
			if (empty($nombre) || empty($desc))
				return false;
			
			$data = array(
				'NombreStatus'   => $nombre,
				'DescStatus'	 => $desc,
			    'dias'			 =>	$dias	
			);
			
			$this->db->insert($this->mTableName, $data);
			return $this->LoadEstatusById($this->db->insert_id());
		}
		
		function UpdateEstatus($estatus) 
		{
			$nombre = $estatus->GetNombre();
			$desc = $estatus->getDesc();
			$dias = $estatus->getDias();
			
			if (empty($nombre) )
				return false;
			
			$data = array(
				'NombreStatus'    	=> $nombre,
				'DescStatus'		=> $desc,
			    'dias'				=>$dias
			);
			
			$this->db->where('IdEstatus', $estatus->getIdEstatus());
        	$this->db->update($this->mTableName, $data);
			
			return true;
		}
		
		function LoadEstatusById($id_estatus) {
			$qresult = $this->db->get_where($this->mTableName, array('IdEstatus' => $id_estatus));

			if ($qresult->num_rows() == 0)
				return false;
	
			return new mestatu($qresult->row());
		}
		
		function LoadEstatus($name = false, $desc = false, $start = false, $offset = false, $sortname = false, $sortorder = false) {
			if($name != false && $name != '')
				$this->db->like('NombreStatus', $name);
			
			if($desc != '')
				$this->db->where('DescStatus', $desc);
			
			if($offset != false)
				$this->db->limit($offset, $start);

			if($sortname != false && $sortorder != false) {
				$this->db->order_by($sortname, $sortorder);
			}
	
			$result = $this->db->get_where($this->mTableName);
	
			return $this->GetEstatusArrayFromQueryResult($result);
		}
		
		function GetCount() {
			return count($this->LoadEstatus());
		}
		
		function GetEstatusArrayFromQueryResult($result) {
			if ($result->num_rows() == 0)
				return false;
	
			$estatus_arr = array();
	
			foreach ($result->result() as $record) {
				$estatus_arr[] = new mestatu($record);
			}
	
			return $estatus_arr;
    	}
		

		
		function Delete($id_estatus) {
			$return = $this->db->delete($this->mTableName, array('IdEstatus' => $id_estatus));
			return $return;
		}
	}
	
	class mestatu {
		private $mIdEstatus = 0;
		
		private $mNombre = "";
		
		private $mdesc = 0;
		
		private $dia = 0;
		
		function __construct($object) {
			$this->setIdEstatus($object->IdEstatus);
			$this->setNombre($object->NombreStatus);
			$this->setDesc($object->DescStatus);
			$this->setDias($object->dias);
		}
		
		private function setIdEstatus($id_estatus) {
			$this->mIdEstatus = $id_estatus;
		}
		
		function getIdEstatus() {
			return $this->mIdEstatus;
		}
		
		function setNombre($nombre) {
			$this->mNombre = $nombre;
		}
		
		function getNombre() {
			return $this->mNombre;
		}
		
		function setDesc($desc) {
			$this->mdesc = $desc;
		}
		
		function getDesc() {
			return $this->mdesc;
		}
		
	    function setDias($desc) {
			$this->dia = $desc;
		}
		
		function getDias() {
			return $this->dia;
		}
		
		
	}
?>